package com.mystudy.atomic.interger;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * TODO: {@link AtomicInteger}测试类
 * @author acy
 * @since 2019/06/04 15:39
 */
public class AtomicIntegerDetailsTest {
	@Test
	public void testCreate(){
		AtomicInteger ai = new AtomicInteger();
		System.out.println(ai.get());
		AtomicInteger ai2 = new AtomicInteger(10);
		System.out.println(ai2);
		//设置值
		ai.set(11);
		System.out.println(ai.get());
		//在真正使用的时候才去设置值
		ai.lazySet(13);
		System.out.println(ai.get());
		//获取并设置值
		AtomicInteger ai3 = new AtomicInteger(1);
		int getValue = ai3.getAndSet(2);
		System.out.println(getValue);
		System.out.println(ai3.get());
		//比较并设置值，比较通过才设置值,通过就返回true，失败就返回false
		boolean compareAndSet = ai3.compareAndSet(1, 2);
		System.out.println(compareAndSet);
	}

	@Test
	public void test() throws Exception{
		synchronized (AtomicIntegerDetailsTest.class){
			System.out.println(Thread.currentThread().getName()+"get lock");
		}
	}
}
